package com.pearadmin.pro.modules.resource.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.resource.entity.ResourceEntity;
import com.pearadmin.pro.modules.resource.entity.ResourceRequest;
import com.pearadmin.pro.modules.resource.mapper.ResourceMapper;
import com.pearadmin.pro.modules.resource.service.ResourceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, ResourceEntity> implements ResourceService {

    @Resource
    private ResourceMapper resourceMapper;

    /**
     * 分页查询所有资源
     *
     * @param request
     * @return java.lang.String
     * @author ZhouXingYun
     * @date 2022/4/18 16:38
     */
    @Override
    public PageResponse page(ResourceRequest request) {
        return Pageable.of(request, (() -> resourceMapper.page(request)));
    }

    /**
     * 检测资源存在
     *
     * @param reName 资源名称
     * @return java.lang.Boolean
     * @author ZhouXingYun
     * @date 2022/4/20 9:28
     */
    @Override
    public Boolean exist(String reName) {
        long count = this.lambdaQuery().eq(ResourceEntity::getReName, reName).count();
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Boolean deleteById(Integer id) {
        int i = resourceMapper.deleteById(id);
        return true;
    }

    @Override
    public Boolean edit(ResourceEntity resourceEntity) {
        return resourceMapper.edit(resourceEntity);

    }

    @Override
    public boolean getReNameCount(String reName, Integer reId) {
        int i = resourceMapper.getReNameCount(reName, reId);
        return i == 0;
    }

    @Override
    public List<String> homeImg() {
        return resourceMapper.homeImg();

    }
}
